package com.tech.langk.testhttps;

import android.os.Handler;
import android.util.Log;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * OKHTTP框架的https请求
 * Created by LangK on 2017/2/3.
 */
public final class SignHttpsUtils {

    private static OkHttpClient client;

//    private static String url = "http://192.168.1.237:8080";
    private static String url = "http://langk.cc:8080";

    /**
     * 发送
     *
     * @throws Exception
     */
    public static void run(final Handler handler, String boby) throws Exception {
        RequestBody body = new FormBody.Builder().add("json", boby).build();
        Request request = new Request.Builder()
                .url(url + "/push").post(body)
                .build();
        client = new OkHttpClient.Builder().build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                handler.sendEmptyMessage(-1);
                Log.d("TAG", "你大爷的，又失败了");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("TAG", "终于成功了");
                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

                Headers responseHeaders = response.headers();
                for (int i = 0; i < responseHeaders.size(); i++) {
                    System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
                }
                String string = response.body().string();
                if (string.equals("true")) {
                    handler.sendMessage(handler.obtainMessage(1,true));
                } else {
                    handler.sendMessage(handler.obtainMessage(1,false));
                }
                System.out.println(string);
            }
        });
    }

    /**
     * 检查
     *
     * @throws Exception
     */
    public static void check(final Handler handler) throws Exception {
        Request request = new Request.Builder()
                .url(url + "/check").get()
                .build();
        client = new OkHttpClient.Builder().build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                handler.sendEmptyMessage(-2);
                Log.d("TAG", "你大爷的，又失败了");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("TAG", "终于成功了");
                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

                Headers responseHeaders = response.headers();
                for (int i = 0; i < responseHeaders.size(); i++) {
                    System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
                }
                String string = response.body().string();
                if (string.equals("true")) {
                    handler.sendMessage(handler.obtainMessage(2,true));
                } else {
                    handler.sendMessage(handler.obtainMessage(2,false));
                }
                System.out.println(string);
            }
        });
    }


    /**
     * 检查
     *
     * @throws Exception
     */
    public static void stop(final Handler handler) throws Exception {
        Request request = new Request.Builder()
                .url(url + "/stop").get()
                .build();
        client = new OkHttpClient.Builder().build();

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                handler.sendEmptyMessage(-3);
                Log.d("TAG", "你大爷的，又失败了");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Log.d("TAG", "终于成功了");
                if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);

                Headers responseHeaders = response.headers();
                for (int i = 0; i < responseHeaders.size(); i++) {
                    System.out.println(responseHeaders.name(i) + ": " + responseHeaders.value(i));
                }
                String string = response.body().string();
                if (string.equals("true")) {
                    handler.sendMessage(handler.obtainMessage(3,true));
                } else {
                    handler.sendMessage(handler.obtainMessage(3,false));
                }
                System.out.println(string);
            }
        });
    }


}